/**
* AutoSWF - Cody Cushing. March 26, 2009
* Copyright (c) 2009 Cody Cushing
* No use restrictions. Maintain this notice in all versions. 
**/

package com.autoswf.dispatcher
{
	import com.autoswf.core.ILink;
	import com.autoswf.core.Link;
	import com.autoswf.core.Transport;
	
	import flash.events.Event;
	
	import flexunit.framework.TestCase;

	public class LinkDispatcherTest extends TestCase
	{
		public function LinkDispatcherTest(methodName:String=null)
		{
			super(methodName);
		}
		
		/**
		 * Tests that an event being passed up the chain will get dispatched if it passes 
		 * through a LinkDispatcher.
		 */		
		public function test01():void
		{
			var handler:Function = function(event:Event):void
			{
				assertStrictlyEquals(testEvent, event);
			}
			
			var testEvent:Event = new Event(Event.CHANGE);
			var linkDispatcher:LinkDispatcher = new LinkDispatcher("link");
			linkDispatcher.addEventListener(Event.CHANGE, addAsync(handler, 20));
			linkDispatcher.sendUp(new Transport(testEvent));
		}
		
		
		/**
		 * Tests that an event passed down will get dispatched if the destination is a LinkDispatcher.
		 */		
		public function test02():void
		{
			var handler:Function = function(event:Event):void
			{
				assertStrictlyEquals(testEvent, event);
			}
			
			var testEvent:Event = new Event(Event.CHANGE);
			var linkDispatcher:LinkDispatcher = new LinkDispatcher("link");
			linkDispatcher.addEventListener(Event.CHANGE, addAsync(handler, 20));
			linkDispatcher.sendDown(new Transport(testEvent));
		}
		
		
		/**
		 * Tests event gets dispatched at its final destination.
		 */		
		public function test03():void
		{
			var handler:Function = function(event:Event):void
			{
				assertStrictlyEquals(testEvent, event);
			}
			
			var testEvent:Event = new Event(Event.CHANGE);
			
			var linkDispatcher:LinkDispatcher = new LinkDispatcher("link_b");
			linkDispatcher.addEventListener(Event.CHANGE, addAsync(handler, 20));
			
			var link:ILink = new Link("link_a");
			link.addChildLink(linkDispatcher);
			link.sendDown(new Transport(testEvent).setPath("link_b"));
		}
	}
}